/**
 * Created by zhanghan on 2017/12/8
 * 调度日志
 */
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/content/systemManager/xxlJobLog/jobLog.js'
], function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    yufp.lookup.reg('TRIGGER_CODE,HANDLE_CODE');
    var queryUrl = backend.jobService + '/api/adminjoblog/querypage';
    if (data.id && data.id != null && data.id != '') {
      queryUrl = backend.jobService + '/api/adminjoblog/querypagebyid?jobId=' + data.id;
    }
    yufp.custom.vue({
      el: cite.el,
      data: function () {
        return {
          formdata: {},
          styleObj: { height: yufp.custom.viewSize().height + 'px', overflow: 'auto' },
          mainGrid: {
            data: null,
            total: null,
            loading: false,
            multipleSelection: [],
            query: {
              jobId: '',
              triggerCode: '',
              handleCode: '',
              jobName: ''
            },
            height: yufp.custom.viewSize().height - 150,
            checkbox: true,
            dataUrl: queryUrl,
            currentRow: null,
            dataParams: {},
            tableColumns: [
              { label: this.$t('jobLog.ddmc'), prop: 'jobDesc', resizable: true},
              { label: this.$t('jobLog.ddsj'), prop: 'triggerTime', resizable: true},
              { label: this.$t('jobLog.dddz'), prop: 'executorAddress', resizable: true },
              { label: this.$t('jobLog.ddjg'), prop: 'triggerCode', width: 100, type: 'select', dataCode: 'TRIGGER_CODE', resizable: true },
              { label: this.$t('jobLog.zxjg'), prop: 'handleCode', width: 100, resizable: true, type: 'select', dataCode: 'HANDLE_CODE' },
              {
                label: this.$t('jobLog.ddbz'),
                prop: 'triggerMsg',
                resizable: true,
                minWidth: 200,
                template: function () {
                  return '<template scope="scope">\
                                <a onclick="return false;" href="javascipt:void(0);" style="text-decoration:underline;" @click="_$event(\'custom-detail-click\', scope)">{{ scope.row.triggerMsg }}</a>\
                                </template>';
                }
              },
              { label: this.$t('jobLog.zxsj'), prop: 'handleTime', type: 'date', resizable: true},
              {
                label: this.$t('jobLog.zxbz'),
                prop: 'handleMsg',
                resizable: true,
                template: function () {
                  return '<template scope="scope">\
                              <a onclick="return false;" href="javascipt:void(0);" style="text-decoration:underline;" @click="_$event(\'custom-msg-click\', scope)">{{ scope.row.handleMsg }}</a>\
                              </template>';
                }
              }
            ]
          },
          dialogFormVisible: false,
          checkDetil: false,
          handleMsgVisible: false
        };
      },
      methods: {
        queryMainGridFn: function () {
          var me = this;
          var param = {
            condition: JSON.stringify({
              triggerCode: this.mainGrid.query.triggerCode ? this.mainGrid.query.triggerCode : null,
              jobName: this.mainGrid.query.jobName ? this.mainGrid.query.jobName : null,
              handleCode: this.mainGrid.query.handleCode ? this.mainGrid.query.handleCode : null
            })
          };
          me.$refs.logtable.remoteData(param);
        },
        openDetailFn: function (row) {
          var me = this;
          if (this.$refs.logtable.selections.length != 1) {
            this.$message({ message: this.$t('jobLog.qxzytjl'), type: 'warning' });
            return;
          }
          if (this.$refs.logtable.selections[0].roleSts == 'A') {
            this.$message({ message: this.$t('jobLog.znxzsxhdsxdsj'), type: 'warning' });
            return;
          }
          var logContent = '';
          var fromLineNum = 1;
          var executorAddress = me.$refs.logtable.selections[0].executorAddress;
          var triggerTime = me.$refs.logtable.selections[0].triggerTime;
          var logId = me.$refs.logtable.selections[0].id;
          var triggerCode = me.$refs.logtable.selections[0].triggerCode;
          if (typeof executorAddress == 'undefined') {
            this.$message({ message: this.$t('jobLog.wfxdddzwfzk'), type: 'warning' });
            return;
          }
          if (typeof triggerCode == 'undefined') {
            this.$message({ message: this.$t('jobLog.wddwfzk'), type: 'warning' });
            return;
          }
          if (typeof triggerTime == 'undefined' || triggerCode != 200) {
            this.$message({ message: this.$t('jobLog.wddcgwfzk'), type: 'warning' });
            return;
          }
          yufp.service.request({
            method: 'get',
            url: backend.jobService + '/api/adminjoblog/logDetailCat',
            data: {
              executorAddress: executorAddress,
              triggerTime: triggerTime,
              logId: logId,
              fromLineNum: fromLineNum
            },
            callback: function (code, message, response) {
              logContent = response.data.logContent;
              me.detailFn(logContent);
            }
          });
        },
        detailFn: function (logContent) {
          var me = this;
          me.checkDetil = true;
          this.$nextTick(function () {
            var xx = me.$refs.checkDetil;
            xx.innerHTML = logContent;
          });
        },
        checkLogFn: function (row) {
          var me = this;
          me.dialogFormVisible = true;
          this.$nextTick(function () {
            var xx = me.$refs.ideEditor;
            xx.innerHTML = row.row.triggerMsg;
          });
        },
        handleMsgShowFn: function (row) {
          var me = this;
          me.handleMsgVisible = true;
          this.$nextTick(function () {
            var xx = me.$refs.handleMsg;
            xx.innerHTML = row.row.handleMsg;
          });
        },
        deleteFn: function () {
          var vue = this;
          if (this.$refs.logtable.selections.length > 0) {
            var id = '';
            for (var i = 0; i < this.$refs.logtable.selections.length; i++) {
              var row = this.$refs.logtable.selections[i];
              id = id + ',' + row.id;
            }
            this.$confirm(this.$t('jobLog.cczjscgrzsfjx'), this.$t('jobLog.ts'), {
              confirmButtonText: this.$t('jobLog.qd'),
              cancelButtonText: this.$t('jobLog.qx'),
              type: 'warning'
            }).then(function () {
              yufp.service.request({
                method: 'POST',
                url: backend.jobService + '/api/adminjoblog/deletebatch',
                data: {
                  id: id
                },
                callback: function (code, message, response) {
                  vue.$message({ message: vue.$t('jobLog.sjsccg') });
                  vue.queryMainGridFn();
                }
              });
            });
          } else {
            this.$message({ message: this.$t('jobLog.qxxzyscdsj'), type: 'warning' });
            return;
          }
        },
        // 终止任务
        logKill: function () {
          var vue = this;
          if (this.$refs.logtable.selections.length != 1) {
            this.$message({ message: this.$t('jobLog.qxzytjl'), type: 'warning' });
            return;
          }
          var row = this.$refs.logtable.selections[0];
          if (row.triggerCode != 200 || row.handleCode != 0) {
            this.$message({ message: this.$t('jobLog.znzzddcgqwzxwbdrw'), type: 'warning' });
            return;
          }
          var id = row.id;
          this.$confirm(this.$t('jobLog.cczjzzgrwsfjx'), this.$t('jobLog.ts'), {
            confirmButtonText: this.$t('jobLog.qd'),
            cancelButtonText: this.$t('jobLog.qx'),
            type: 'warning'
          }).then(function () {
            yufp.service.request({
              method: 'POST',
              url: backend.jobService + '/api/adminjoblog/logKill',
              data: {
                idStr: id
              },
              callback: function (code, message, response) {
                vue.$message({ message: vue.$t('jobLog.rwzzcg') });
                vue.queryMainGridFn();
              }
            });
          });
        }
      }
    });
  };
});
